 package srdb.migration.dbhandler.srdb;
 
   
import srdb.migration.MTKException;
import srdb.migration.common.MTKTransformDDL;
import srdb.migration.schema.metadata.Column;
import srdb.migration.schema.metadata.Constraint;
import srdb.migration.schema.metadata.DataType;
import srdb.migration.schema.metadata.Domain;
import srdb.migration.schema.metadata.Function;
import srdb.migration.schema.metadata.Grant;
import srdb.migration.schema.metadata.Group;
import srdb.migration.schema.metadata.Index;
import srdb.migration.schema.metadata.Package;
import srdb.migration.schema.metadata.Procedure;
import srdb.migration.schema.metadata.Role;
import srdb.migration.schema.metadata.Rule;
import srdb.migration.schema.metadata.Synonym;
import srdb.migration.schema.metadata.Table;
import srdb.migration.schema.metadata.Trigger;
import srdb.migration.schema.metadata.User;
import srdb.migration.schema.metadata.View;
 
 public class SRDBTransformDDL extends MTKTransformDDL
 {
   public String getCreateScript(Table table)
     throws MTKException
   {
     String sql = super.getCreateScript(table);
 
     if (table.isInherited()) {
       sql = sql + " INHERITS (";
 
       for (int i = 0; i < table.getInheritedFromList().size(); i++) {
         Table parentTable = (Table)table.getInheritedFromList().get(i);
         sql = sql + parentTable.getFullName();
         if (i != table.getInheritedFromList().size() - 1) {
           sql = sql + ",";
         }
       }
 
       sql = sql + ") ";
     }
 
     boolean hasOID = table.isRelHasOIDs();
     boolean hasFillFactor = (table.getFillFactor() != null) && (!table.getFillFactor().trim().equals(""));
     if ((hasOID) && (hasFillFactor)) {
       sql = sql + " WITH (OIDS=TRUE ";
       sql = sql + ",FILLFACTOR= " + table.getFillFactor();
       sql = sql + ")";
     } else if (hasOID) {
       sql = sql + " WITH (OIDS=TRUE) ";
     } else if (hasFillFactor) {
       sql = sql + "WITH (FILLFACTOR= " + table.getFillFactor();
       sql = sql + ")";
     }
 
     sql = sql + ";\n";
     return sql;
   }
 
   protected String getColumnLine(Column column)
   {
     String str = "";
     String colName = column.getName();
 
     if ((!column.isCaseSensitive()) && (colName.indexOf('#') >= 0)) {
       colName = "\"" + colName.toLowerCase() + "\"";
     }
 
     str = str + getIndent() + colName + " " + column.getDataTypeString();
 
     if (((column.getDataType() == Column.DataType.VARCHAR) || (column.getDataType() == Column.DataType.NVARCHAR)) && (column.getLength() > 0))
     {
       str = str + "(" + column.getLength() + ")";
     }
     if (((column.getDataType() == Column.DataType.INTEGER) && (!column.isWholeNumberType())) || ((column.getDataType() == Column.DataType.NUMERIC) && (!column.isReal())))
     {
       if (column.getPrecision() > 0) {
         str = str + "(" + column.getPrecision();
         if (column.getScale() > 0)
           str = str + "," + column.getScale();
         str = str + ")";
       }
 
     }
 
     if (column.getDefaultClause() != null) {
       str = str + " DEFAULT " + column.getDefaultClause();
     }
 
     if (!column.isNullable()) {
       str = str + " NOT NULL";
     }
     return str;
   }
 
   public String getCreateScript(Constraint constraint)
   {
     String sql = "";
     String constraintType = String.valueOf(constraint.getType());
 
     if (constraintType.equalsIgnoreCase("P")) {
       sql = sql + "ALTER TABLE " + constraint.getTableName() + " ADD CONSTRAINT " + constraint.getName(true) + " PRIMARY KEY (";
 
       for (int i = 0; i < constraint.getColumns().size(); i++) {
         sql = sql + constraint.getColumns().get(i).getName();
         if (i < constraint.getColumns().size() - 1)
           sql = sql + ", ";
       }
       sql = sql + ");\n";
     } else if (constraintType.equalsIgnoreCase("U")) {
       sql = sql + "ALTER TABLE " + constraint.getTableName() + " ADD CONSTRAINT " + constraint.getName(true) + " UNIQUE (";
 
       for (int i = 0; i < constraint.getColumns().size(); i++) {
         sql = sql + constraint.getColumns().get(i).getName();
         if (i < constraint.getColumns().size() - 1) {
           sql = sql + ", ";
         }
       }
       sql = sql + ")";
 
       String fillFactor = constraint.getFillFactor();
 
       if ((fillFactor != null) && (fillFactor.trim().length() > 0)) {
         sql = sql + " WITH (FILLFACTOR = " + fillFactor + ")";
       }
       sql = sql + ";\n";
     } else if (constraintType.equalsIgnoreCase("C"))
     {
       if (!constraint.getSearchCondition().endsWith("IS NOT NULL")) {
         sql = sql + "ALTER TABLE " + constraint.getTableName() + " ADD CONSTRAINT " + constraint.getName(true) + " CHECK (" + constraint.getSearchCondition();
 
         sql = sql + ");\n";
       }
     } else if ((constraintType.equalsIgnoreCase("R")) || (constraintType.equalsIgnoreCase("F"))) {
       sql = sql + "ALTER TABLE " + constraint.getSchemaQualifiedTableName() + " ADD CONSTRAINT " + constraint.getName(true) + " FOREIGN KEY (";
 
       for (int i = 0; i < constraint.getColumns().size(); i++) {
         sql = sql + constraint.getColumns().get(i).getName();
         if (i < constraint.getColumns().size() - 1)
           sql = sql + ", ";
       }
       sql = sql + ") REFERENCES " + constraint.getRefIntegritySchemaQualifiedTableName() + " (";
       for (int i = 0; i < constraint.getRefIntegrityColumns().size(); i++) {
         sql = sql + constraint.getRefIntegrityColumns().get(i).getName();
         if (i < constraint.getRefIntegrityColumns().size() - 1) {
           sql = sql + ", ";
         }
       }
       sql = sql + ") MATCH ";
       String matchType = constraint.getFkMatchType();
 
       if (matchType.equalsIgnoreCase("f"))
         sql = sql + "FULL ";
       else if (matchType.equalsIgnoreCase("p"))
         sql = sql + "PARTIAL ";
       else {
         sql = sql + "SIMPLE ";
       }
 
       sql = sql + "ON DELETE " + getFKRuleClause(constraint.getDeleteRule());
 
       sql = sql + " ON UPDATE " + getFKRuleClause(constraint.getUpdateRule()) + ";\n";
     }
 
     return replaceNames(sql, getSourceToTargetNames());
   }
 
   private String getFKRuleClause(String charValue)
   {
     if (charValue.equalsIgnoreCase("r"))
       return "RESTRICT";
     if (charValue.equalsIgnoreCase("c"))
       return "CASCADE";
     if (charValue.equalsIgnoreCase("n"))
       return "SET NULL";
     if (charValue.equalsIgnoreCase("d")) {
       return "SET DEFAULT";
     }
     return "NO ACTION";
   }
 
   public String getCreateScript(Index index)
   {
     if (!index.isUnique()) {
       String sql = index.getSql() + ";\n";
 
       return replaceNames(sql, getSourceToTargetNames());
     }
     return "";
   }
 
   public String getCreateScript(Trigger trigger)
   {
     return trigger.getBody();
   }
 
   public String getCreateScript(Synonym syn) {
     String sql = "CREATE " + (syn.isPublic() ? "PUBLIC" : "") + " SYNONYM " + syn.getSynname() + "\n";
     sql = sql + getIndent() + " FOR " + syn.getSynobjname() + ";\n";
     return sql;
   }
 
   public String getCreateScript(Procedure proc) {
     throw new UnsupportedOperationException("Procedures are not supported in PostgreSQL.");
   }
 
   public String getCreateScript(Function func) {
     return func.getSQL();
   }
 
   public String getCreateScriptPackageSpec(Package pack)
   {
     throw new UnsupportedOperationException("Packages are not supported in PostgreSQL.");
   }
 
   public String getCreateScriptPackageBody(Package pack)
   {
     throw new UnsupportedOperationException("Packages are not supported in PostgreSQL.");
   }
 
   public String getCreateScript(Role role)
   {
     return role.getSQL();
   }
 
   public String getCreateScript(User user)
   {
     return "";
   }
 
   public String getCreateScript(Grant grant)
   {
     String str = "GRANT " + grant.getPrivilege() + " ON " + grant.getObjectName() + " TO " + grant.getGrantee() + (grant.isGrantable() ? " WITH GRANT OPTION" : "");
     return str;
   }
 
   public String getCreateScript(Rule rule)
   {
     return rule.getDefinition();
   }
 
   public String getCreateScript(Group group)
   {
     return "CREATE GROUP " + group.getName();
   }
 
   public String getMembershipScript(Group group)
   {
     String sql = "";
 
     if ((group.getMembers() != null) && (group.getMembers().length > 0)) {
       for (int i = 0; i < group.getMembers().length; i++) {
         if (i > 0) {
           sql = sql + ";\n";
         }
         sql = sql + "GRANT " + group.getMembers()[i] + " TO " + group.getName();
       }
     }
 
     return sql;
   }
 
   public String getCreateScript(Domain domain)
   {
     if (domain == null)
       return null;
     String sql = "CREATE DOMAIN " + domain.getName() + " AS " + domain.getDataType();
     if (domain.getDefaultValue() != null) {
       sql = sql + "\nDEFAULT " + domain.getDefaultValue();
     }
     sql = sql + domain.getConstraints();
     sql = sql + ";";
     return sql;
   }
 
   public String getCreateScript(DataType type)
   {
     if (type == null)
       return null;
     String sql = "CREATE TYPE ";
     sql = sql + type.getName();
     if (type.getTypType() == DataType.TypType.ENUMERATION) {
       sql = sql + " AS ENUM ( ";
       sql = sql + type.getQuotedLableList();
       sql = sql + " )";
     }
     else if (type.getTypType() == DataType.TypType.COMPOSITE) {
       sql = sql + " AS ( ";
       sql = sql + type.getQuotedColumnList();
       sql = sql + " )";
     } else {
       sql = "";
     }
     return sql;
   }
 
   public String getCreateScript(View view)
   {
     return replaceNames(super.getCreateScript(view), getSourceToTargetNames());
   }
 }

/* 

 * 
 */